package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.WorkerMapper;
import com.pojo.TbSupplier;
import com.pojo.TbWorker;
import com.service.WorkerService;
import com.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WorkerServiceImpl extends ServiceImpl<WorkerMapper, TbWorker> implements WorkerService {
    @Autowired
    JSONResult jsonResult;

    @Override
    public JSONResult findAllWorkerByWorkerName(int page, int limit, String workerName) {
        Page<TbWorker> tbpage = new Page<>(page,limit);
        if (workerName!=null&&workerName!=""){
            QueryWrapper<TbWorker> tbWorker = new QueryWrapper<>();
            tbWorker.eq("worker_name",workerName);
            Page<TbWorker> page1 = this.page(tbpage,tbWorker);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }
        Page<TbWorker> page1 = this.page(tbpage);
        return jsonResult.success(201,"查询失败,名字不能为空",page1,page1.getTotal());
    }

    @Override
    public JSONResult findAllWorkerByState(int page, int limit, int state) {
        Page<TbWorker> tbpage = new Page<>(page,limit);
        if (state==1&&state!=-1){
            QueryWrapper<TbWorker> tbWorker = new QueryWrapper<>();
            tbWorker.eq("worker_state",state);
            Page<TbWorker> page1 = this.page(tbpage,tbWorker);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }else if (state==0&&state!=-1){
            Page<TbWorker> page1 = this.page(tbpage);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }
        return jsonResult.failure(201,"状态码错误,查询失败",null,null);
    }

    @Override
    public JSONResult addWorker(TbWorker worker) {
        boolean save = this.save(worker);
        if (save){
            return jsonResult.success(200,"添加成功",null,1L);
        }
        return jsonResult.failure(201,"添加失败",null,0L);
    }

    @Override
    public JSONResult deleteWorker(int workerId) {
        boolean b = this.removeById(workerId);
        if (b){
            return jsonResult.success(200,"删除成功",null,null);
        }
        return jsonResult.failure(201,"删除失败",null,null);
    }

    @Override
    public JSONResult updateWorker(TbWorker worker) {
        boolean b = this.saveOrUpdate(worker);
        if (b){
            return jsonResult.success(200,"修改成功",null,null);
        }
        return jsonResult.failure(201,"修改失败",null,null);
    }

    @Override
    public JSONResult findWorkerById(int workerId) {
        QueryWrapper<TbWorker> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("worker_id",workerId);
        TbWorker worker = this.getOne(queryWrapper);
        if (worker!=null){
            return jsonResult.success(200,"查询成功",worker,null);
        }
        return jsonResult.failure(201,"查询失败",null,null);
    }

    @Override
    public JSONResult findAllWorker(int page, int limit) {
        Page<TbWorker> tbpage = new Page<>(page,limit);
        QueryWrapper<TbWorker> tbProducts = new QueryWrapper<>();
        Page<TbWorker> page1 = this.page(tbpage,tbProducts);
        if (page1.getSize()>0)
        return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        return jsonResult.success(201,"查询失败",page1,page1.getTotal());
    }
}
